<template>
  <div>
    <!--  按钮组  -->
    <div class="button_group">
      <el-button plain size="mini" style="margin: 20px 20px 20px 35px;" type="success" @click="addUser">添加用户</el-button>
    </div>
    <!--  数据显示  -->
    <el-table :data="tableList" border empty-text="数据空,请添加" style="width: 96%;margin: 20px auto 0;">
      <el-table-column align="center" label="Id" width="60">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户名" width="80">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户头像" width="160">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" alt="" style="width: 80px;height: 60px;display: block;border-radius: 20%;margin: 0 auto;box-shadow: 0 0 10px gray"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户账号" width="120">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户权限组" width="100">
        <template slot-scope="scope">
          {{ scope.row.role_name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="用户介绍">
        <template slot-scope="scope">
          <div v-if="scope.row.introduction == null || scope.row.introduction === ''">
            <el-tag type="danger">该用户暂无介绍,请管理员添加介绍</el-tag>
          </div>
          <div v-else>{{ scope.row.introduction }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.updated_at }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="scope">
          <el-button plain size="mini" style="margin-right: 10px" type="primary" @click="changeUserInfo(scope.row.id)">
            修改用户
          </el-button>
          <el-popconfirm cancel-button-text='取消' confirm-button-text='确认' icon="el-icon-info" icon-color="red" title="确认删除?" @confirm="confirmDelete(scope.row.id)">
            <el-button slot="reference" plain size="mini" type="danger">删除用户</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--  数据修改  -->
    <el-dialog :visible.sync="dialogFormVisible" title="管理员账号修改">
      <el-form :model="getEditUserInfo">
        <el-form-item :label-width="formLabelWidth" label="用户账号">
          <el-input v-model="getEditUserInfo.account" auto-complete="off" placeholder="修改用户账号"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户名">
          <el-input v-model="getEditUserInfo.username" auto-complete="off" placeholder="修改用户名"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="密码">
          <el-input v-model="getEditUserInfo.password" auto-complete="off" placeholder="修改密码"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户头像">
          <!--<el-input v-model="getEditUserInfo.username" auto-complete="off"></el-input>-->
          <img :src="getEditUserInfo.avatar" alt="" style="width: 60px;height: 60px;border-radius: 50%"/>
          <Upload></Upload>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户简介">
          <el-input v-model="getEditUserInfo.introduction" auto-complete="off" placeholder="输入用户简介"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="账号状态">
          <el-switch v-model="changeInfoBoolean" active-color="#13ce66" active-text="正常" inactive-color="#ff4949" inactive-text="隐藏" @change="switchChange"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmChange(getEditUserInfo)">确 定</el-button>
      </div>
    </el-dialog>
    <!--  数据添加  -->
    <el-dialog :visible.sync="dialogFormVisible2" title="管理员账号添加">
      <el-form :model="addEditUserInfo">
        <el-form-item :label-width="formLabelWidth" label="用户账号">
          <el-input v-model="addEditUserInfo.account" auto-complete="off" placeholder="添加用户账号"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户名">
          <el-input v-model="addEditUserInfo.username" auto-complete="off" placeholder="添加用户名"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="密码">
          <el-input v-model="addEditUserInfo.password" auto-complete="off" placeholder="输入密码"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户头像">
          <!--<el-input v-model="getEditUserInfo.username" auto-complete="off"></el-input>-->
          <img :src="addEditUserInfo.avatar" alt="" style="width: 60px;height: 60px;border-radius: 50%"/>
          <Upload></Upload>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="用户简介">
          <el-input v-model="addEditUserInfo.introduction" auto-complete="off" placeholder="输入用户简介"></el-input>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="账号状态">
          <el-switch v-model="changeInfoBoolean2" active-color="#13ce66" active-text="正常" inactive-color="#ff4949" inactive-text="隐藏" @change="switchChange2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmChange(getEditUserInfo)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as mission from '@/api/permission'
import { Message } from "element-ui";

export default {
  name: "Permission",
  data () {
    return {
      tableList: null,
      changeInfoBoolean: true,
      changeInfoBoolean2: true,
      addEditUserInfo: {
        username: null,
        account: null,
        password: null,
        role_id: null,
        avatar: null,
        introduction: null,
        status: null
      },
      getEditUserInfo: {},
      formLabelWidth: '120px',
      dialogFormVisible: false,
      dialogFormVisible2: false
    }
  },
  created () {
    this.fetchAdminList ()
  },
  methods: {
    // 获取管理员列表
    async fetchAdminList () {
      await mission.getUserList ().then (( response ) => {
        const {data} = response.data
        this.tableList = data
      })
    },
    //  点击修改用户按钮
    async changeUserInfo ( id ) {
      await mission.editorAdminInfo (id).then (( response ) => {
        this.dialogFormVisible = true;
        this.changeInfoBoolean = response.data.status === 1;
        this.getEditUserInfo = response.data
      })
    },
    // 确认修改管理员账号信息
    async confirmChange ( data ) {
      const url = data.avatar
      // let domain = url.split ('/')
      if (this.$store.state.upload.imgUrl == null || this.$store.state.upload.imgUrl === '') {
        this.getEditUserInfo.avatar = url
        await mission.editorAdminChange (data).then (() => {
          Message.success ({
            message: '修改成功'
          })
          this.dialogFormVisible = false
          this.fetchAdminList ()
        })
      } else {
        this.getEditUserInfo.avatar = this.$store.state.upload.imgUrl
        await mission.editorAdminChange (data).then (() => {
          Message.success ({
            message: '修改成功'
          })
          this.dialogFormVisible = false
          this.fetchAdminList ()
        })
      }
    },
    //  删除用户
    confirmDelete ( id ) {
      this.deleteUser (id)
    },
    async deleteUser ( id ) {
      await mission.deleteAdmin (id).then (() => {
        Message.info ({
          message: '删除成功'
        })
      })
    },
    //  修改switch需改
    switchChange ( e ) {
      switch (e) {
        case true:
          this.getEditUserInfo.status = 1
          break;
        case false:
          this.getEditUserInfo.status = -1
          break
      }
    },
    switchChange2 ( e ) {
      switch (e) {
        case true:
          this.addEditUserInfo.status = 1
          break;
        case false:
          this.addEditUserInfo.status = 1
          break
      }
    },
    // 添加用户按钮
    addUser () {
      this.dialogFormVisible2 = true
    }
  }
}
</script>

<style scoped>

</style>
